import promise from "../utils/wx_promise.js"

class Native {

  servicesList = []
  deviceFoundList = {}
  onValueChangeList = {}
  onAdapterStateChange = null
  connectStateChangeList = {}

  constructor() {
    //this.onGlobalEvent()
  }

  onGlobalEvent() {

    wx.onBluetoothDeviceFound(this.onDeviceScan.bind(this))
    wx.onBLEConnectionStateChange(this.onDeviceConnectStateChange.bind(this))
    wx.onBluetoothAdapterStateChange(this.onBluetoothAdapterStateChange.bind(this))
    wx.onBLECharacteristicValueChange(this.onValueChange.bind(this))

  }

  connectStateChangeListPush(arg) {
    this.connectStateChangeList.push(arg)
  }
  deviceFoundListPush(arg) {
    this.deviceFoundList.push(arg)
  }

  onDeviceScan(deviceObj) {
    for (let key in this.deviceFoundList) {
      this.deviceFoundList[key](deviceObj)
    }
    
    /* this.deviceFoundList.forEach(cb => {
      cb(deviceObj)
    }) */
  }

  onDeviceConnectStateChange({ deviceId, connected }) {
    for (let key in this.connectStateChangeList) {
      this.connectStateChangeList[key]({ deviceId, connected })
    }
    /* this.connectStateChangeList.forEach(cb => {
      cb({ deviceId, connected })
    }) */
  }

  onBluetoothAdapterStateChange({ available, discovering }) {
    this.onAdapterStateChange && this.onAdapterStateChange({ available, discovering})
  }

  onValueChangePush(cb) {
    this.onValueChangeList.push(cb)
  }

  //特征值变更
  onValueChange(data) {
    for (let key in this.onValueChangeList) {
      this.onValueChangeList[key](data)
    }
    /* this.onValueChangeList.forEach(cb => {
      cb(data)
    }) */
  }

  openAdapter() {
    return promise(wx.openBluetoothAdapter)()
  }

  closeAdapter() {
    return promise(wx.closeBluetoothAdapter)()
  }

  startDiscovery(services = []) {
    return promise(wx.startBluetoothDevicesDiscovery)({
      allowDuplicatesKey: true,
      //services
    })
  }

  stopDiscovery() {
    return promise(wx.stopBluetoothDevicesDiscovery)()
  }

  createConnect({ deviceId, timeout}) {
    let obj = { deviceId }

    if (timeout !== undefined) {
      obj.timeout = timeout
    }

    return promise(wx.createBLEConnection)(obj)
  }

  closeConnect(deviceId) {
    return promise(wx.closeBLEConnection)({
      deviceId
    })
  }

  deviceService(deviceId) {
    return promise(wx.getBLEDeviceServices)({
      deviceId
    })
  }

  deviceCharacteristics(deviceId, serviceId) {
    // console.log("getBLEDeviceCharacteristics", deviceId, serviceId)
    return promise(wx.getBLEDeviceCharacteristics)({
      deviceId,
      serviceId
    })
  }

  notifyBLECharacteristicValueChange(deviceId, serviceId, characteristicId) {
    console.log("notifyBLECharacteristicValueChange", deviceId, serviceId, characteristicId)
    return promise(wx.notifyBLECharacteristicValueChange)({
      deviceId,
      serviceId,
      characteristicId,
      state: true,
    })
  }
  connectedBluetoothDevices(services) {
    return promise(wx.getConnectedBluetoothDevices)({
      services
    })
  }

}

export default Native